package com.wanmait.movie.controller;

import com.wanmait.movie.entity.Admin;
import com.wanmait.movie.entity.LobbyType;
import com.wanmait.movie.entity.request.manage.lobbyType.AddLobbyTypeRequest;
import com.wanmait.movie.entity.request.manage.lobbyType.UpLobbyTypeRequest;
import com.wanmait.movie.service.ManageLobbyTypeService;
import com.wanmait.movie.util.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * ClassName: ManageTypeController
 * Package: com.wanmait.movie.controller
 * Description:
 * 影厅类型管理
 * @Author Fengxd
 * @Create 2023/12/8 13:54
 * @Version 1.0
 */
@Validated
@RestController
@RequestMapping("manage")
public class ManageLobbyTypeController {
    @Resource
    private ManageLobbyTypeService manageLobbyTypeService;

    /**
     * 获取影厅类型信息
     * @return 影厅类型信息
     */
    @GetMapping("lobbyTypeInfo")
    public AjaxResult getTypeInfo(){
        return AjaxResult.success(manageLobbyTypeService.getLobbyTypeInfo());
    }

    /**
     * 添加影厅信息
     * @param addLobbyTypeRequest 影厅类型信息
     */
    @PostMapping("addLobbyType")
    public AjaxResult addLobbyType(@RequestBody @Validated AddLobbyTypeRequest addLobbyTypeRequest){
        LobbyType lobbyType = new LobbyType();
        BeanUtils.copyProperties(addLobbyTypeRequest,lobbyType);
        if (!manageLobbyTypeService.save(lobbyType)) return AjaxResult.error();
        return AjaxResult.success("添加成功");
    }

    /**
     * 修改影厅类型信息
     * @param upLobbyTypeRequest
     * @return AjaxResult
     */

    @PostMapping("upLobbyType")
    public AjaxResult upLobbyType(@RequestBody @Validated UpLobbyTypeRequest upLobbyTypeRequest,
                                  HttpServletRequest request){
        LobbyType lobbyType = new LobbyType();
        BeanUtils.copyProperties(upLobbyTypeRequest,lobbyType);
        lobbyType.setUpdateTime(new Date());
        Admin admin = (Admin) request.getAttribute("admin");
        // 更新人id
        lobbyType.setUpdateAdminId(admin.getId());
        if (!manageLobbyTypeService.updateById(lobbyType)) return AjaxResult.error();
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除影厅类型
     * @param id 影厅id
     * @return 删除结果
     */
    @GetMapping("delLobbyType")
    public AjaxResult delLobbyType(@RequestParam Integer id){
        if (!manageLobbyTypeService.removeById(id)) return AjaxResult.error();
        return AjaxResult.success();
    }
}
